<%@ page import="
	java.util.*,
	java.math.BigDecimal,
	com.probiz.estore.Constants,
	com.probiz.estore.order.model.OnlinePaymentResponse,
	com.probiz.estore.common.model.system.PaymentGateway,
	com.probiz.estore.system.service.PaymentGatewayManager,
	com.probiz.estore.common.service.OrderService,
	org.springframework.web.context.support.WebApplicationContextUtils,
	org.springframework.web.context.WebApplicationContext"
	pageEncoding="UTF-8"%>

<%
	WebApplicationContext webAppContext = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
	PaymentGatewayManager paymentGatewayManager = (PaymentGatewayManager) webAppContext.getBean("paymentGatewayManager");
	PaymentGateway paymentGateway = paymentGatewayManager.getByPaymentGatewayCode("ips");
	HashMap paramMap = paymentGateway.getConfigData();
	String merCert =paramMap.get("merCert").toString().trim();		//商户证书
	String orderNo = request.getParameter("billno");				//订单号
	String amount =request.getParameter("amount");					//订单金额
	String createTime = request.getParameter("date");				//订单日期
	String currency	= request.getParameter("Currency_type");		//币种
	String succ=request.getParameter("succ");
	String msg=request.getParameter("msg");
	String attach=request.getParameter("attach");
	String ipsbillno=request.getParameter("ipsbillno");
	String retEncodeType=request.getParameter("retencodetype");
	String signature=request.getParameter("signature");

	boolean verify = false;
	//验证方式： 12-md5
	if(retEncodeType.equals("12"))
	{
		//【明文：订单编号+订单金额+订单日期+成功标志+IPS订单编号+币种
		StringBuffer sb = new StringBuffer();
		sb.append(orderNo).append(amount).append(createTime).append(succ).append(ipsbillno).append(currency).append(merCert);
		cryptix.jce.provider.MD5 b=new cryptix.jce.provider.MD5();
		String signMD5 = b.toMD5(sb.toString()).toLowerCase();
		if(signMD5.equals(signature)) 
		{
			verify = true;
		}
	}
	Short flag = Constants.PAYMENT_HISTORY_FLAG_FAILURE;
	if(verify)
	{
		if (succ!=null){
			if(succ.equalsIgnoreCase("Y")){
			 	//out.println("交易成功");
			 	flag = Constants.PAYMENT_HISTORY_FLAG_SUCCESS; 
			}else{
			  	//out.println("交易失败");
			}
			 
			OnlinePaymentResponse onlinePaymentResponse = new OnlinePaymentResponse();
			onlinePaymentResponse.setFlowNo(ipsbillno);   //必填. 如果支付网关没有流水号，则使用原始的orderNo，即未decode前的
			onlinePaymentResponse.setOrderNo(orderNo);  //必填
			onlinePaymentResponse.setFlag(flag);    //必填
			onlinePaymentResponse.setAmount(new BigDecimal(amount));  //必填
			onlinePaymentResponse.setPaymentGateway(paymentGateway);  //必填
			onlinePaymentResponse.setPayer(null);
			OrderService orderService = (OrderService) webAppContext.getBean("orderService");
			request.setAttribute("onlinePaymentResponse",onlinePaymentResponse);
			orderService.doHandleOnlinePaymentResponse(onlinePaymentResponse,request);
		}
		
	}else{
	 	//out.println("验证失败");
	}

%>

<%@include file="./include/finishPayResult.jspf"%>